package com.yh.workbill.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.workbill.R;
import com.yh.workbill.adapter.BottomCompanySelectAdapter;

import java.util.List;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class BottomCompanySelectDialog extends Dialog {

    private Activity activity;
    private boolean cancel;
    private RecyclerView recyclerView;
    private List<String> dataList;
    private BottomCompanySelectAdapter adapter;
    private LinearLayout ll_container;
    private TextView tv_cancel;
    private OnSelect onSelect;

    public void setOnSelect(OnSelect onSelect) {
        this.onSelect = onSelect;
    }

    public void setCancel(boolean cancel) {
        this.setCanceledOnTouchOutside(cancel);
    }

    public BottomCompanySelectDialog(Activity activity, List<String> dataList) {
        super(activity, R.style.ActionSheetDialogStyle);
        this.activity = activity;
        this.dataList = dataList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_bottom);

        ll_container = findViewById(R.id.ll_container);
        recyclerView = findViewById(R.id.recyclerView);
        tv_cancel = findViewById(R.id.tv_cancel);
        setViewLocation();

        adapter = new BottomCompanySelectAdapter(dataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(adapter);


        setOnDismissListener(dialog -> dismiss());

        adapter.setOnItemClick(value -> {
            if (onSelect != null) {
                onSelect.onSelect(value);
                dismiss();
            }
        });

        setOnDismissListener(dialog -> {
            if (onSelect != null) {
                onSelect.onDismiss();
            }
        });
        tv_cancel.setOnClickListener(v -> dismiss());
    }

    public interface OnSelect {
        void onSelect(String value);

        void onDismiss();
    }

    /**
     * 设置dialog位于屏幕底部
     */
    private void setViewLocation() {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;

//        ll_container.setMinimumHeight(height / 3);

        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        lp.y = height;
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        onWindowAttributesChanged(lp);
    }
}
